/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-10-13 10:58:05
 * @LastEditTime: 2025-10-13 16:15:16
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\lc\handheld\th_app.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include <math.h>
#include "handheld.h"
#include "rng.h"
#include "internal_temp_sensor.h"
#include "bsp_lvgl.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"

static const char *TAG = "th_app";

extern lv_obj_t *icon_in_obj; // 应用界面
extern int icon_flag;

extern i2c_obj_t i2c0_master;

static lv_timer_t *my_lv_timer;

static lv_obj_t *meter_temp;
static lv_obj_t *meter_humi;
static lv_obj_t *label_temp;
static lv_obj_t *label_humi;

static lv_meter_indicator_t *temp_indic;
static lv_meter_indicator_t *humi_indic;

LV_FONT_DECLARE(font_alipuhui20);
LV_FONT_DECLARE(font_awesome);

int temp_value, humi_value; // 室内实时温湿度值

/**
 * @description: 返回主界面向上滑动事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void my_gesture_event_cb(lv_event_t *e)
{
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    if (dir == LV_DIR_TOP)
    {
        // printf("my_gesture_event_cb\n");
        lv_obj_del(icon_in_obj); //删除画布
        lv_timer_del(my_lv_timer);
        icon_flag = 0;
    }
}

/**
 * @description:  定时更新温湿度值
 * @param {lv_timer_t *} timer
 * @return {*}
 */
static void thv_update_cb(lv_timer_t * timer)
{
    lv_meter_set_indicator_end_value(meter_temp, temp_indic, temp_value);
    lv_meter_set_indicator_end_value(meter_humi, humi_indic, humi_value);
    lv_label_set_text_fmt(label_temp, "%02d℃", temp_value);
    lv_label_set_text_fmt(label_humi, "%d%%", humi_value);
}

/**
 * @description: 获取温湿度任务
 * @param {void} *arg
 * @return {*}
 */
static void get_th_task(void *arg)
{
    esp_err_t ret;
    int time_cnt = 0, date_cnt = 0;
    float temp_sum = 0.0, humi_sum = 0.0;
    float temp=0, humi=0;

    while(1)
    {
        temp = sensor_get_temperature();
        humi = (float)rng_get_random_range(60, 98);    //76-81随机数，虚拟湿度值

        temp_sum = temp_sum + temp; // 温度累计和
        humi_sum = humi_sum + humi; // 湿度累计和
        date_cnt++; // 记录累计次数

        vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100毫秒
        time_cnt++; // 每100毫秒+1
        if(time_cnt>10) // 1秒钟到
        {
            // 取平均数 且把结果四舍五入为整数
            temp_value = round(temp_sum/date_cnt); 
            humi_value = round(humi_sum/date_cnt); 
            // 各标志位清零
            time_cnt = 0; date_cnt = 0; temp_sum = 0; humi_sum = 0;
            ESP_LOGD(TAG, "TEMP:%d HUMI:%d", temp_value, humi_value);
        }
        if (icon_flag == 0)
        {
            break;
        }
    }

    temperature_sensor_deinit();
    vTaskDelete(NULL);
}

/**
 * @description: 温湿度应用
 * @return {*}
 */
void th_app(void)
{
    temperature_sensor_init();

    temp_value = round(sensor_get_temperature()); 
    humi_value = rng_get_random_range(60, 98); 

    lvgl_port_lock(0);

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 10);
    lv_style_set_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_color_hex(0x00BFFF));
    lv_style_set_bg_grad_color(&style, lv_color_hex(0x00BF00));
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);
    lv_style_set_border_width(&style, 0);
    lv_style_set_pad_all(&style, 0);
    lv_style_set_width(&style, 320);
    lv_style_set_height(&style, 240);

    // 创建一个界面
    icon_in_obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(icon_in_obj, &style, 0);

    // 温度仪表部件
    meter_temp = lv_meter_create(icon_in_obj);
    lv_obj_center(meter_temp);
    lv_obj_set_size(meter_temp, 220, 220);
    lv_obj_remove_style(meter_temp, NULL, LV_PART_INDICATOR);

    // 刻度
    lv_meter_scale_t* scale = lv_meter_add_scale(meter_temp); /* 定义并添加刻度 */
    lv_meter_set_scale_ticks(meter_temp, scale, 9, 2, 10, lv_palette_main(LV_PALETTE_GREY)); /* 设置小刻度 */
    lv_meter_set_scale_major_ticks(meter_temp, scale, 1, 2, 12, lv_palette_main(LV_PALETTE_GREY), 12); /* 设置主刻度 */    
    lv_meter_set_scale_range(meter_temp, scale, -30, 50, 160, 190); /* 设置仪表数值范围、有效角度和旋转角度 */
    
    lv_meter_indicator_t *indic;

    /* 添加弧线指示器，弧线宽度为 3，颜色为蓝色，偏移 0 */
    indic = lv_meter_add_arc(meter_temp, scale, 3, lv_palette_main(LV_PALETTE_BLUE), 0);
    lv_meter_set_indicator_start_value(meter_temp, indic, -30);
    lv_meter_set_indicator_end_value(meter_temp, indic, 18);
    /* 添加指示刻度，起始颜色为蓝色，终止颜色为蓝色，状态为 false，指示刻度为 0 */
    indic = lv_meter_add_scale_lines(meter_temp, scale, lv_palette_main(LV_PALETTE_BLUE),lv_palette_main(LV_PALETTE_BLUE), false, 0);
    lv_meter_set_indicator_start_value(meter_temp, indic, -30);
    lv_meter_set_indicator_end_value(meter_temp, indic, 18);

    /* 添加弧线指示器，弧线宽度为 3，颜色为绿色，偏移 0 */
    indic = lv_meter_add_arc(meter_temp, scale, 3, lv_palette_main(LV_PALETTE_GREEN), 0);
    lv_meter_set_indicator_start_value(meter_temp, indic, 18);
    lv_meter_set_indicator_end_value(meter_temp, indic, 25);
    /* 添加指示刻度，起始颜色为绿色，终止颜色为绿色，状态为 false，指示刻度为 0 */
    indic = lv_meter_add_scale_lines(meter_temp, scale, lv_palette_main(LV_PALETTE_GREEN),lv_palette_main(LV_PALETTE_GREEN), false, 0);
    lv_meter_set_indicator_start_value(meter_temp, indic, 18);
    lv_meter_set_indicator_end_value(meter_temp, indic, 25);

    /* 添加弧线指示器，弧线宽度为 3，颜色为红色，偏移 0 */
    indic = lv_meter_add_arc(meter_temp, scale, 3, lv_palette_main(LV_PALETTE_RED), 0);
    lv_meter_set_indicator_start_value(meter_temp, indic, 25);
    lv_meter_set_indicator_end_value(meter_temp, indic, 50);
    /* 添加指示刻度，起始颜色为红色，终止颜色为红色，状态为 false，指示刻度为 0 */
    indic = lv_meter_add_scale_lines(meter_temp, scale, lv_palette_main(LV_PALETTE_RED),lv_palette_main(LV_PALETTE_RED), false, 0);
    lv_meter_set_indicator_start_value(meter_temp, indic, 25);
    lv_meter_set_indicator_end_value(meter_temp, indic, 50);

    /* 添加弧线指示器，弧线宽度为 10，颜色为红色，偏移 13 */
    temp_indic = lv_meter_add_arc(meter_temp, scale, 10, lv_palette_main(LV_PALETTE_ORANGE), 13);
    lv_meter_set_indicator_start_value(meter_temp, temp_indic, -30);
    lv_meter_set_indicator_end_value(meter_temp, temp_indic, temp_value);

    // 温度图标
    lv_obj_t *label_temp_symbol = lv_label_create(meter_temp);
    lv_label_set_text(label_temp_symbol, "\xEF\x8B\x88");
    lv_obj_set_style_text_font(label_temp_symbol, &font_awesome, 0);
    lv_obj_align(label_temp_symbol, LV_ALIGN_CENTER, -28, -30);

    // 温度
    label_temp = lv_label_create(meter_temp);
    lv_label_set_text_fmt(label_temp, "%02d℃", temp_value);
    lv_obj_set_style_text_font(label_temp, &font_alipuhui20, 0);
    lv_obj_align_to(label_temp, label_temp_symbol, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    // 湿度仪表部件
    meter_humi = lv_meter_create(icon_in_obj);
    lv_obj_center(meter_humi);
    lv_obj_set_size(meter_humi, 220, 220);
    lv_obj_set_style_opa(meter_humi, LV_OPA_50, 0);
    lv_obj_remove_style(meter_humi, NULL, LV_PART_INDICATOR);

    lv_meter_scale_t* scale_humi = lv_meter_add_scale(meter_humi); /* 定义并添加刻度 */
    lv_meter_set_scale_ticks(meter_humi, scale_humi, 11, 2, 10, lv_palette_main(LV_PALETTE_GREY)); /* 设置小刻度 */
    lv_meter_set_scale_major_ticks(meter_humi, scale_humi, 1, 2, 12, lv_palette_main(LV_PALETTE_GREY), 12); /* 设置主刻度 */    
    lv_meter_set_scale_range(meter_humi, scale_humi, 0, 100, 160, 10); /* 设置仪表数值范围、有效角度和旋转角度 */

    /* 添加弧线指示器，弧线宽度为 3，颜色为蓝色，偏移 0 */
    indic = lv_meter_add_arc(meter_humi, scale_humi, 3, lv_palette_main(LV_PALETTE_BLUE), 0);
    lv_meter_set_indicator_start_value(meter_humi, indic, 0);
    lv_meter_set_indicator_end_value(meter_humi, indic, 48);
    /* 添加指示刻度，起始颜色为蓝色，终止颜色为蓝色，状态为 false，指示刻度为 0 */
    indic = lv_meter_add_scale_lines(meter_humi, scale_humi, lv_palette_main(LV_PALETTE_BLUE),lv_palette_main(LV_PALETTE_BLUE), false, 0);
    lv_meter_set_indicator_start_value(meter_humi, indic, 0);
    lv_meter_set_indicator_end_value(meter_humi, indic, 48);

    /* 添加弧线指示器，弧线宽度为 3，颜色为绿色，偏移 0 */
    indic = lv_meter_add_arc(meter_humi, scale_humi, 3, lv_palette_main(LV_PALETTE_GREEN), 0);
    lv_meter_set_indicator_start_value(meter_humi, indic, 40);
    lv_meter_set_indicator_end_value(meter_humi, indic, 70);
    /* 添加指示刻度，起始颜色为绿色，终止颜色为绿色，状态为 false，指示刻度为 0 */
    indic = lv_meter_add_scale_lines(meter_humi, scale_humi, lv_palette_main(LV_PALETTE_GREEN),lv_palette_main(LV_PALETTE_GREEN), false, 0);
    lv_meter_set_indicator_start_value(meter_humi, indic, 40);
    lv_meter_set_indicator_end_value(meter_humi, indic, 70);

    /* 添加弧线指示器，弧线宽度为 3，颜色为红色，偏移 0 */
    indic = lv_meter_add_arc(meter_humi, scale_humi, 3, lv_palette_main(LV_PALETTE_RED), 0);
    lv_meter_set_indicator_start_value(meter_humi, indic, 70);
    lv_meter_set_indicator_end_value(meter_humi, indic, 100);
    /* 添加指示刻度，起始颜色为红色，终止颜色为红色，状态为 false，指示刻度为 0 */
    indic = lv_meter_add_scale_lines(meter_humi, scale_humi, lv_palette_main(LV_PALETTE_RED),lv_palette_main(LV_PALETTE_RED), false, 0);
    lv_meter_set_indicator_start_value(meter_humi, indic, 70);
    lv_meter_set_indicator_end_value(meter_humi, indic, 100);

    /* 添加弧线指示器，弧线宽度为 10，颜色为红色，偏移 13 */
    humi_indic = lv_meter_add_arc(meter_humi, scale_humi, 10, lv_palette_main(LV_PALETTE_ORANGE), 13);
    lv_meter_set_indicator_start_value(meter_humi, humi_indic, 0);
    lv_meter_set_indicator_end_value(meter_humi, humi_indic, humi_value);

    // 湿度图标
    lv_obj_t *label_humi_symbol = lv_label_create(meter_temp);
    lv_obj_set_style_text_font(label_humi_symbol, &font_awesome, 0);
    lv_label_set_text(label_humi_symbol, "\xEF\x81\x83");
    lv_obj_align(label_humi_symbol, LV_ALIGN_CENTER, -28, 30);
   
    // 湿度
    label_humi = lv_label_create(meter_temp);
    lv_label_set_text_fmt(label_humi, "%d%%", humi_value);
    lv_obj_set_style_text_font(label_humi, &font_alipuhui20, 0);
    lv_obj_align_to(label_humi, label_humi_symbol, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    // 返回提示符
    lv_obj_t *label_back = lv_label_create(icon_in_obj);
    lv_label_set_text(label_back, LV_SYMBOL_UP);
    lv_obj_set_style_text_color(label_back, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_back, &lv_font_montserrat_20, 0);
    lv_obj_align(label_back, LV_ALIGN_BOTTOM_RIGHT, -20, -20);

    // 添加向上滑动退出功能
    lv_obj_add_event_cb(icon_in_obj, my_gesture_event_cb, LV_EVENT_GESTURE, 0);
    lv_obj_clear_flag(icon_in_obj, LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_flag(icon_in_obj, LV_OBJ_FLAG_CLICKABLE);

    lvgl_port_unlock();

    icon_flag = 7; // 标记已经进入第七个应用

    // 创建一个获取温湿度的任务
    xTaskCreatePinnedToCore(get_th_task, "get_th_task", 4 * 1024, NULL, 5, NULL, 1);
    // 创建一个lv_timer 定时更新数据
    my_lv_timer = lv_timer_create(thv_update_cb, 1000, NULL);      
}